package com.msec.net.flutter;

import android.content.Context;

import java.lang.reflect.Method;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

@SuppressWarnings("unchecked")
public class FlutterPluginMsec implements FlutterPlugin, MethodCallHandler {
    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
    private MethodChannel channel;
    private Method method;

    private static void initialized(Context context) {
        try {
            Class c = Class.forName("com.msec.net.flutter.FlutterProxy");
            Method m = c.getDeclaredMethod("initialized", Context.class);
            m.invoke(null, context);
        } catch (Throwable t) {
            // do nothing
        }
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onAttachedToEngine(FlutterPluginBinding flutterPluginBinding) {
        // may be invoked more than one time
        channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "flutter_plugin_msec");
        channel.setMethodCallHandler(this);
        initialized(flutterPluginBinding.getApplicationContext());
    }

    // This static function is optional and equivalent to onAttachedToEngine. It supports the old
    // pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
    // plugin registration via this function while apps migrate to use the new Android APIs
    // post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
    //
    // It is encouraged to share logic between onAttachedToEngine and registerWith to keep
    // them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
    // depending on the user's project. onAttachedToEngine or registerWith must both be defined
    // in the same class.
    public static void registerWith(Registrar registrar) {
        final MethodChannel channel = new MethodChannel(registrar.messenger(), "flutter_plugin_msec");
        channel.setMethodCallHandler(new FlutterPluginMsec());
        initialized(registrar.context().getApplicationContext());
    }

    @Override
    public void onMethodCall(MethodCall call, Result result) {
        try {
            if (method == null) {
                Class c = Class.forName("com.msec.net.flutter.FlutterProxy");
                method = c.getDeclaredMethod("onMethodCall", MethodCall.class, Result.class);
            }
            method.invoke(null, call, result);
        } catch (Throwable t) {
            // do nothing
        }
    }

    @Override
    public void onDetachedFromEngine(FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
    }
}
